// 2023-03-14 10:51:14

import { request } from "express";
import { response } from "express";
import CODE from "../config/data.config.js";

/**
 * @param {Error} err 
 * @param {request} req 
 * @param {response} res 
 */
function errorWare(err, req, res, next) {
    try {
        res.removeHeader('token')
        if (typeof (err) === 'string') {
            res.send({
                msg: err,
                code: CODE.CODE_FAIL
            }).end()
            return
        }
        const { status } = err
        res.status(parseInt(status) || 403)
        res.send({
            msg: err.message,
            name: err.name,
            stack: err.stack,
            code: CODE.CODE_FAIL
        })
    } catch (error) {
        // 记录日记
        console.log(error)
        res.send(error).end()
    }
}

export default errorWare